package com.huatai.saas.mapper;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.huatai.saas.entity.SceneFileEntity;
import com.huatai.saas.vo.SceneFileAuthInfoVo;
import com.huatai.saas.vo.SceneFileAuthUserVo;
import com.huatai.saas.vo.SceneFileCollectUserVo;
import com.huatai.saas.vo.SceneFileHistoryInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author hecai
* @description 针对表【scene_file(场景列表)】的数据库操作Mapper
* @createDate 2024-04-28 10:02:00
* @Entity .SceneFile
*/
@Mapper
public interface SceneFileMapper extends BaseMapper<SceneFileEntity> {

	/**
	 * 用户获取用户场景列表
	 * @param page
	 * @return
	 */
	@Select(" SELECT  " +
		" b.id,a.id as usaId,  " +
		"  b.file_name,  " +
		"  b.total_size,  " +
		"  b.description,  " +
		"  b.industry_key,  " +
		"  a.auth_start_time,  " +
		"  a.auth_end_time,  " +
		"  a.`status`   " +
		"FROM  " +
		"  user_scene_auth AS a  " +
		"  INNER JOIN scene_file AS b ON a.sf_id = b.id   " +
		"WHERE  " +
		"  is_deleted = 0   " +
		"  AND user_id = ${userId}   " +
		"  AND file_name LIKE '%${fileName}%' " +
		" AND b.industry_key like '%${industryKey}%' " +
		" and a.auth_end_time>='${nowDate}' " +
		" order by b.create_time desc ")
	IPage<SceneFileAuthUserVo> getSceneFilePageList(IPage<SceneFileAuthUserVo> page,
													@Param("userId") Long  userId,
													@Param("industryKey") String  industryKey,
													@Param("nowDate") String nowDate,
													@Param("fileName") String  fileName);

	/**
	 * 用户分页获取收藏场景列表
	 * @param page
	 * @param wrapper
	 * @return
	 */
	@Select(" SELECT " +
		" b.id, " +
		" b.file_name, " +
		" b.total_size, " +
		" b.description,b.path, " +
		" b.price,b.picture_path as picturePath  " +
		"FROM " +
		" scene_file_collect AS a " +
		" INNER JOIN scene_file AS b ON a.sf_id = b.id "+
		" where is_deleted = 0 and user_id = ${userId} and " +
		" file_name LIKE '%${fileName}%' and industry_key LIKE '%${industryKey}%' ")
	IPage<SceneFileCollectUserVo> getSceneFileCollectPageList(IPage<SceneFileCollectUserVo> page,
															  @Param("userId") Long  userId,
															  @Param("industryKey") String  industryKey,
															  @Param("fileName") String  fileName);

	/**
	 * 根据场景id获取场景历史版本
	 * @param id
	 * @return
	 */
	@Select(" SELECT " +
		" a.id,a.file_name, " +
		" b.description, " +
		" b.path, " +
		" b.total_size, " +
		" b.version,a.industry_key, " +
		" b.create_time " +
		"FROM " +
		" `scene_file` AS a " +
		" LEFT JOIN scene_file_history AS b ON a.id = b.sf_id  " +
		"WHERE " +
		" a.id = ${id}  " +
		" AND a.is_deleted =0 " +
		" order by b.create_time desc ")
	List<SceneFileHistoryInfoVo> getSceneFileHistory(@Param("id")Long id);

	/**
	 * 分页获取场景是否被授权
	 * @param page
	 * @param userId
	 * @return
	 */
	@Select("<script>" +
		" SELECT " +
		" a.file_name, " +
		" a.picture_path, " +
		" a.total_size, " +
		" a.description, " +
		" a.price,a.industry_key, " +
		" a.id,b.user_id,a.create_time  " +
		"FROM " +
		" `scene_file` AS a " +
		" LEFT JOIN user_scene_auth AS b ON a.id = b.sf_id  " +
		"WHERE " +
		" a.is_deleted = 0 and a.file_name LIKE '%${fileName}%' and a.industry_key LIKE '%${industryKey}%'  AND b.auth_end_time >= '${authEndTime}'   " +
		" <if test='userId!=null '>" +
		" and b.user_id = ${userId}  " +
		"</if>" +
		"  ORDER BY a.create_time DESC  "+
		"</script>" )
	IPage<SceneFileAuthInfoVo> getIsAuthPageDataInfo(IPage<SceneFileAuthInfoVo> page,
													 @Param("userId")Long userId,
													 @Param("industryKey") String  industryKey,
													 @Param("fileName") String  fileName,@Param("authEndTime") String  authEndTime);


	/**
	 * 分页获取场景授权列表
	 * @param page
	 * @param userId
	 * @return
	 */
	@Select("<script>" +
		" SELECT " +
		" *  " +
		"FROM " +
		" ( " +
		" SELECT " +
		"  a.id, " +
		"  a.create_time, " +
		"  a.description, " +
		"  a.file_name, " +
		"  a.open_id, " +
		"  a.price, " +
		"  a.total_size, " +
		"  a.update_time, " +
		"  a.version, a.industry_key, " +
		" CASE " +
		"    " +
		"   WHEN ( " +
		"   SELECT " +
		"    count( 1 )  " +
		"   FROM " +
		"    user_scene_auth AS b  " +
		"   WHERE " +
		"    b.user_id = ${userId}  " +
		"    AND b.sf_id = a.id  " +
		"    AND b.auth_end_time >= '${authEndTime}'  " +
		"    ) > 0 THEN " +
		"    '1' ELSE '0'  " +
		"   END AS selectStatus  " +
		"  FROM " +
		"   scene_file AS a  " +
		"  WHERE " +
		"  a.is_deleted = 0  " +
		" ) AS T " +
		" where T.file_name like '%${fileName}%' and T.industry_key LIKE '%${industryKey}%'  "+
		" <if test='selectStatus!=null and selectStatus != 0 '>" +
		" and T.selectStatus=0   " +
		"</if>" +
		" ORDER BY T.create_time DESC "+
		"</script>" )
	IPage<SceneFileAuthInfoVo> getIsAuthPageData(IPage<SceneFileAuthInfoVo> page,
												 @Param("userId")Long userId,
												 @Param("fileName")String fileName,
												 @Param("industryKey")String industryKey,
												 @Param("selectStatus")String selectStatus,
												 @Param("authEndTime")String authEndTime);


	@Select(" SELECT  " +
		"  a.file_name,  " +
		"  a.picture_path,  " +
		"  a.total_size,  " +
		"  a.description,  " +
		"  a.price,  " +
		"  a.id,a.create_time,  " +
		"  b.user_id   " +
		"FROM  " +
		"  `scene_file` AS a  " +
		"  LEFT JOIN scene_file_collect AS b ON a.id = b.sf_id   " +
		"WHERE  " +
		"  a.is_deleted = 0 ")
	IPage<SceneFileAuthInfoVo> getCollectPageData(IPage<SceneFileAuthInfoVo> page);
}
